{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Chat_Bot_Medium_Code.ipynb",
      "version": "0.3.2",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "k8SLbj96pC23",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Description: This is a chatbot program\n",
        "\n",
        "#There are broadly two variants of chatbots: Rule-Based and Self learning.\n",
        "#Rule-based approach, a bot answers questions based on some rules on which it is trained on\n",
        "#Self learning bots are the ones that use some Machine Learning-based approach to chat"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-ddmLsiwafao",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#import libraries\n",
        "from nltk.chat.util import Chat, reflections"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FoLapbnVai3a",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Pairs is a list of patterns and responses.\n",
        "pairs = [\n",
        "    [\n",
        "        r\"(.*)my name is (.*)\",\n",
        "        [\"Hello %2, How are you today ?\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)help(.*) \",\n",
        "        [\"I can help you \",]\n",
        "    ],\n",
        "     [\n",
        "        r\"(.*) your name ?\",\n",
        "        [\"My name is J.A.R.V.I.S like in Iron Man, but you can just call me Jarvis and I'm a chatbot .\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"how are you (.*) ?\",\n",
        "        [\"I'm doing very well\", \"i am great !\"]\n",
        "    ],\n",
        "    [\n",
        "        r\"sorry (.*)\",\n",
        "        [\"Its alright\",\"Its OK, never mind that\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"i'm (.*) (good|well|okay|ok)\",\n",
        "        [\"Nice to hear that\",\"Alright, great !\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(hi|hey|hello|hola|holla)(.*)\",\n",
        "        [\"Hello\", \"Hey there\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"what (.*) want ?\",\n",
        "        [\"Make me an offer I can't refuse\",]\n",
        "        \n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)created(.*)\",\n",
        "        [\"randerson112358 created me using Python's NLTK library \",\"top secret ;)\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*) (location|city) ?\",\n",
        "        ['Tokyo, Japan',]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)raining in (.*)\",\n",
        "        [\"No rain in the past 4 days here in %2\",\"In %2 there is a 50% chance of rain\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"how (.*) health (.*)\",\n",
        "        [\"Health is very important, but I am a computer, so I don't need to worry about my health \",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)(sports|game|sport)(.*)\",\n",
        "        [\"I'm a very big fan of Basketball\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"who (.*) (moviestar|actor|actress)?\",\n",
        "        [\"Zendaya\"]\n",
        "    ],\n",
        "    [\n",
        "        r\"quit\",\n",
        "        [\"Bye for now. See you soon :) \",\"It was nice talking to you. See you soon :)\"]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)\",\n",
        "        ['That is nice to hear']\n",
        "    ],\n",
        "]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xJ0uZNvls4BK",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 286
        },
        "outputId": "a4229825-4dd8-40d2-ac4e-00445168b3c1"
      },
      "source": [
        "#This is a dictionary that contains a set of input values and its corresponding output values. \n",
        "#It is an optional dictionary that you can use unless you want to use regular expressions. \n",
        "reflections"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'i': 'you',\n",
              " 'i am': 'you are',\n",
              " 'i was': 'you were',\n",
              " \"i'd\": 'you would',\n",
              " \"i'll\": 'you will',\n",
              " \"i'm\": 'you are',\n",
              " \"i've\": 'you have',\n",
              " 'me': 'you',\n",
              " 'my': 'your',\n",
              " 'you': 'me',\n",
              " 'you are': 'I am',\n",
              " 'you were': 'I was',\n",
              " \"you'll\": 'I will',\n",
              " \"you've\": 'I have',\n",
              " 'your': 'my',\n",
              " 'yours': 'mine'}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h8E_XLhlvxrC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Create your own reflections\n",
        "my_dummy_reflections= {\n",
        "    \"go\"     : \"gone\",\n",
        "    \"hello\"    : \"hey there\"\n",
        "}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Tl1oJ_C7avNg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 170
        },
        "outputId": "7a1d5e43-7d4e-4428-aa74-3a81a28182f8"
      },
      "source": [
        "#default message at the start of chat\n",
        "print(\"Hi, I'm Jarvis and I like to chat\\nPlease type lowercase English language to start a conversation. Type quit to leave \") \n",
        "\n",
        "#Create Chat Bot\n",
        "chat = Chat(pairs, reflections)\n",
        "\n",
        "#chat._substitute('i am a programmer') #uncomment this line to see reflections example in action\n",
        "\n",
        "#Start conversation\n",
        "chat.converse()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hi, I'm Jarvis and I like to chat\n",
            "Please type lowercase English language to start a conversation. Type quit to leave \n",
            ">hi jarvis my name is randerson112358\n",
            "Hello randerson112358, How are you today ?\n",
            ">i'm doing well, i was wondering if you could help me with something ?\n",
            "I can help you \n",
            ">great, i wanted to know what's your favorite sport ?\n",
            "I'm a very big fan of Basketball\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}